#ifndef CUSTOMFILEITEM_H
#define CUSTOMFILEITEM_H

#include <QWidget>
#include <QIcon>
#include <QPixmap>
#include <QFont>
#include <QFontMetrics>
#include <QString>
#include <QSize>
#include <QPoint>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QEnterEvent>
#include <QPainter>
#include <QApplication>
#include "filestatusiconmanager.h"
#include "unifiedfileinfo.h"

/**
 * CustomFileItem - 自定义文件项控件
 * 
 * 基于QWidget的轻量级文件项显示组件
 * 替代复杂的QTreeWidgetItem，解决数据与UI同步问题
 * 
 * 现在使用 UnifiedFileInfo 作为数据模型
 */

class CustomFileItem : public QWidget
{
    Q_OBJECT

public:
    explicit CustomFileItem(const UnifiedFileInfo& fileInfo, QWidget* parent = nullptr);
    explicit CustomFileItem(QWidget* parent = nullptr);  // 【新增】虚拟化架构：无数据构造函数
    ~CustomFileItem() override;
    
    // === 布局常量（虚拟化需要公开访问）===
    static constexpr int ITEM_HEIGHT = 48;
    static constexpr int ICON_SIZE = 40;
    static constexpr int MARGIN = 10;
    static constexpr int SPACING = 6;
    static constexpr int CORNER_RADIUS = 6;
    static constexpr int PROGRESS_LEFT_OFFSET = 140;
    
    static constexpr int COMPACT_ITEM_HEIGHT = 40;
    static constexpr int COMPACT_ICON_SIZE = 32;
    static constexpr int COMPACT_MARGIN = 6;
    static constexpr int COMPACT_SPACING = 4;
    
    static constexpr int FINDER_ITEM_HEIGHT = 22;
    static constexpr int FINDER_ICON_SIZE = 16;
    static constexpr int FINDER_MARGIN = 4;
    static constexpr int FINDER_SPACING = 4;
    
    // 默认行宽（现在支持自适应）
    static constexpr int DEFAULT_ITEM_WIDTH = 480;
    static constexpr int MIN_ITEM_WIDTH = 100;  // 最小宽度调整为100px，支持更紧凑的显示
    
    // 数据管理
    void setFileInfo(const UnifiedFileInfo& fileInfo);
    const UnifiedFileInfo& getFileInfo() const { return m_fileInfo; }
    
    // 选中状态管理
    void setSelected(bool selected);
    bool isSelected() const { return m_isSelected; }
    
    // 悬停状态管理
    void setHovered(bool hovered);
    bool isHovered() const { return m_isHovered; }
    
    // 尺寸管理
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;
    
    // 自适应尺寸管理
    void setAdaptiveWidth(int width);  // 设置自适应宽度
    int getAdaptiveWidth() const { return m_adaptiveWidth; }
    
    // 获取布局参数（根据宽度自动调整）
    struct LayoutParams {
        int itemHeight;
        int iconSize;
        int margin;
        int spacing;
        int fontSize;
        bool showProgress;
        bool showLayoutInfo;
    };
    LayoutParams getLayoutParams() const;

signals:
    void clicked(CustomFileItem* item, Qt::KeyboardModifiers modifiers);
    void doubleClicked(CustomFileItem* item);
    void rightClicked(CustomFileItem* item, const QPoint& globalPos);
    void dragStartRequested(CustomFileItem* item, const QPoint& startPos);

protected:
    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseDoubleClickEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void enterEvent(QEnterEvent* event) override;
    void leaveEvent(QEvent* event) override;

private:
    UnifiedFileInfo m_fileInfo;
    bool m_isSelected = false;
    bool m_isHovered = false;
    
    // 拖拽相关
    QPoint m_dragStartPos;
    bool m_dragStarted = false;
    Qt::KeyboardModifiers m_pressModifiers = Qt::NoModifier;
    
    // 自适应尺寸支持
    int m_adaptiveWidth = DEFAULT_ITEM_WIDTH;
    
    // 辅助方法
    void initializeUI();
    QString getLayoutModeText() const;
    QIcon getStatusIcon() const;
    
    // 绘制方法
    void drawBackground(QPainter& painter);
    void drawIcon(QPainter& painter);
    void drawTexts(QPainter& painter);
    void drawStatusBadge(QPainter& painter);
};

#endif // CUSTOMFILEITEM_H